<script type="text/javascript">
$(function(){
	var pg = App.base.appInstEdit.ui, pp = pg.params || {};

	var frm = {
		no: pg.seek('no'),
		deployPath: pg.seek('deployPath'),
		tempPath: pg.seek('tempPath'),
		port: pg.seek('port'),
		user: pg.seek('user'),
		appCode: pg.seek('appCode'),
		appName: pg.seek('appName'),
		serverCode: pg.seek('serverCode'),
		serverName: pg.seek('serverName')
	};

	pg.seek('pickServer').click(function(){
		pg.hide();
		App.open({
			title: '请选择一个服务器',
			ctx: 'base',
			action: 'serverPicker',
			params: {
				env: pp.env,
				multi: 0,
				projId: pp.app.projId,
				id: pp.app.id
			},
			model: true,
			onClose: function(){
				pg.show();
			}
		}, function(s){
			pp.server = s;
			frm.serverCode.val(s.code);
			frm.serverName.val(s.name);
		});
		return false;
	});
	function getValues(){
		return {
			id: pp.id,
			serverId: pp.server? pp.server.id:'',
			appId: pp.app.id,
			serverCode: frm.serverCode.val(),
			serverName: frm.serverName.val(),
			deployPath: frm.deployPath.val(),
			tempPath: frm.tempPath.val(),
			port: frm.port.val(),
			user: frm.user.val(),
			no: frm.no.val()
		};
	}
	function setValues(o){
		frm.appCode.val(o.app.code);
		frm.appName.val(o.app.name)
		if (o.server) {
			frm.serverCode.val(o.server.code);
			frm.serverName.val(o.server.name);
		}
		frm.deployPath.val(o.deployPath);
		frm.tempPath.val(o.tempPath);
		frm.port.val(o.port);
		frm.user.val(o.user);
		frm.no.val(o.no);
	}
	function validate(p){
		var x = App.validate, v=App.base.validation,
			msg = pg.seek('msg').html('');
		return x(v.app.inst.serverCode, p.serverCode, msg, '服务器编号')
			&& x(v.app.inst.serverName, p.serverName, msg, '服务器名称')
			&& x(v.app.inst.deployPath, p.deployPath, msg, '部署目录')
			&& x(v.app.inst.tempPath, p.tempPath, msg, '临时目录')
			&& x(v.app.inst.user, p.user, msg, '用户');
	}
	pg.find('input').blur(function(){
		var pms = getValues();
		if (!validate(pms)) {
			return;
		}
	});
	pg.seek('ok').click(function(){
		var pms = getValues();
		if (!validate(pms)) {
			return;
		}
		$.post(App.ctx + '/app/inst/save.do', pms, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				pg.callback(ro.data);//更新树	
				pg.close();
			} else {
				pg.seek('msg').html(mac.msg(ro.data, ro.params));
			}
		});
		return false;
	});
	pg.seek('close').click(function(){
		pg.close();
	});
	setValues(pp);
});
</script>
<div class="left">
	<div class="form">
		<div class="item">
			<span class="label">应用编号：</span>
			<input type="text" name="appCode" placeholder="应用编号" disabled />
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">应用名称：</span>
			<input type="text" name="appName" placeholder="应用名称" disabled />
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">服务器编号：</span>
			<input type="text" name="serverCode" placeholder="请选择一个服务器" disabled />
			<div class="picker" name="pickServer">
				<span class="icon">&nbsp;</span>
			</div>
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">服务器名称：</span>
			<input type="text" name="serverName" placeholder="服务器名称" disabled />
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">部署路径：</span>
			<input type="text" name="deployPath" placeholder="应用安装根路径" />
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">临时目录：</span>
			<input type="text" name="tempPath" placeholder="部署时的临时目录" />
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">用户：</span>
			<input type="text" name="user" placeholder="应用的所属用户" />
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">端口号：</span>
			<input type="number" name="port" placeholder="应用主端口（可选）" />
			<!-- span class="msg"></span>  -->
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">实例号：</span>
			<input type="number" name="no" placeholder="实例号" min="0" />
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label"></span>
			<span class="msg" name="msg"></span>
			<div class="clear"></div>
		</div>
		<div class="bottom">
			<button name="ok" class="addBtn">确定</button>
			<button name="close" class="addBtn">取消</button>
		</div>
	</div>
</div>
